package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.TeamMapper;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWage;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyWageMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-07
 */
@Service
public class BtyWageService extends ServiceImpl<BtyWageMapper, BtyWage> {
    @Resource
    private UserMapper userMapper;

    @Resource
    private TeamMapper teamMapper;
    /**
     * 获取通知列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:05 PM
     */

    public Page<Map<String, Object>> list(String condition,String dm,String tel,String month,String status,String username,String ddName,String flag,Integer isCompliance, String startDate, String endDate) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.list(page, condition,dm,tel,month,status,username,ddName,flag,isCompliance,startDate,endDate);
    }

    public List listGz(String condition,String dm,String tel,String month,String status,String username,String ddName,String flag) {

        return this.baseMapper.listGz(condition,dm,tel,month,status,username,ddName,flag);
    }

    public List<BtyArchives> dmList(String username,String ddName,String phone, Integer isCompliance) {
        return this.baseMapper.dmList(username,ddName,phone, isCompliance);
    }

    public List<Map> dmListMap(String username,String ddName,String phone) {
        return this.baseMapper.dmListMap(username,ddName,phone);
    }

    public List<Map<String, Object>> listExport(String condition,String dm,String tel,String month,String status,String username,String ddName,String flag, Integer isCompliance,String startDate,String endDate) {
        return this.baseMapper.listExport(condition,dm,tel,month,status,username,ddName,flag,isCompliance,startDate,endDate);
    }

    public List<BtyWage> wageList(String month,String xm,String sfz) {
        return this.baseMapper.wageList(month, xm, sfz);
    }
    public  String sumWage(String month,String xm,String sfz){
        return baseMapper.sumWage(month, xm, sfz);
    }
    public List<BtyWage> wageLists(String openid,String month) {
        return this.baseMapper.wageLists(openid,month);
    }
    public  String sumWages(String openid,String month){
        return baseMapper.sumWages(openid,month);
    }

    public Page<Map<String, Object>> listWage(String condition,String dm,String month,String bh) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.listWage(page, condition,dm,month,bh);
    }

     public  List<Map<String, Object>>listWageExport(String condition,String dm,String month,String bh){
        return baseMapper.listWageExport(condition,dm,month,bh);
    }

    public int queryAllDaibanQty(String account) {
        String condition = null;
        String dm = null;
        String status = "1";
        int list=0;
        if (ToolUtil.isNotEmpty(account)){
            //获取用户角色
            User user = userMapper.getByAccount(account);
            if(user==null){
                return 0;
            }
            List<String> role = teamMapper.getRole(account);
            if (ToolUtil.isNotEmpty(user) || ToolUtil.isNotEmpty(role)){
                String username = user.getName();//用户名字
                String roleId = user.getRoleId();//用户角色id
                List rolds = Arrays.asList(roleId.split(","));
                //获取用户角色名称
                List<String> roleNames = userMapper.getRole(rolds);
                //获取当前月的上个月
                String lastMonth = DateUtil.getLastMonth();
                String flag = "";
                if(!("admin".equals(account))){
                    flag="8";
                    //根据姓名查询是否是督导
                    if(!roleNames.contains("督导")){
                        list = this.baseMapper.queryAllDaibanQty(condition,dm,null,lastMonth,status,username,null,flag);
                        if(list == 0){
                            list = this.baseMapper.queryAllDaibanQty(condition,dm,account,lastMonth,status,null,null,flag);
                        }
                    }else{
                        list = this.baseMapper.queryAllDaibanQty(condition,dm,null,lastMonth,status,null,username,flag);
                        if(list == 0){
                            list = this.baseMapper.queryAllDaibanQty(condition,dm,account,lastMonth,status,null,username,flag);
                        }
                    }
                }else{
                    list = this.baseMapper.queryAllDaibanQty(condition,dm,null,lastMonth,status,null,null,flag);
                }
            }
        }
        return list;
    }

   public Page<Map<String, Object>> hpList(String month,String dm){
        Page page = LayuiPageFactory.defaultPage();
        return baseMapper.hpList(page,month,dm);
    }
   public List<Map<String, Object>>listHpExport(String month,String dm){
        return baseMapper.listHpExport(month,dm);
   }


}
